package com.itcast.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itcast.constant.MessageConstant;
import com.itcast.constant.RedisMessageConstant;
import com.itcast.entity.Result;
import com.itcast.utils.SMSUtils;
import com.itcast.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class validateCodeController {

    @Autowired
    private JedisPool jedisPool;

    //用户发送验证码
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        //随机生成四位验证码
        String param = ValidateCodeUtils.generateValidateCode(4).toString();
        /*try {
            //调用工具类发送验证码
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,param);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }*/
        System.out.println(param);
        //将验证码保存到redis中(5分钟有效)
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,param);

        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    //用户快速登录发送验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        //随机生成四位验证码
        String param = ValidateCodeUtils.generateValidateCode(6).toString();
        /*try {
            //调用工具类发送验证码
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,param);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }*/
        System.out.println(param);
        //将验证码保存到redis中(5分钟有效)
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,param);

        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
